package Form

import (
	"github.com/lxn/walk"
	"github.com/lxn/walk/declarative"
)

func (m *FormModule) LoginMainForm() {
	var username, password *walk.LineEdit
	mw, err := walk.NewMainWindow()
	if err != nil {
		m.logger.Fatalln("LoginUI create error")
		return
	}
	declarative.MainWindow{
		AssignTo: &mw,
		Title:    "登录",
		Size:     declarative.Size{Width: loginGUIWidth, Height: loginGUIHeight},
		Layout:   declarative.VBox{},
		Children: []declarative.Widget{
			declarative.Composite{
				Layout: declarative.Grid{Columns: 2},
				Children: []declarative.Widget{
					declarative.Label{
						Text: "                            用户名：",
					},
					declarative.LineEdit{
						AssignTo: &username,
						MaxSize:  declarative.Size{Width: 150, Height: 40},
					},
				},
			},
			declarative.Composite{
				Layout: declarative.Grid{Columns: 2},
				Children: []declarative.Widget{
					declarative.Label{
						Text: "                            密    码：",
					},
					declarative.LineEdit{
						AssignTo:     &password,
						MaxSize:      declarative.Size{Width: 150, Height: 40},
						PasswordMode: true,
					},
				},
			},
			declarative.Composite{
				Layout: declarative.HBox{},
				Children: []declarative.Widget{
					declarative.PushButton{
						Text:    "登录",
						MaxSize: declarative.Size{Width: 50, Height: 40},
						OnClicked: func() {
							flag := m.loginModule.LoginService(username.Text(), password.Text())
							if flag {
								m.loginModule.SaveLoginStatus()
								mw.Close()
								m.MainForm()
							} else {
								walk.MsgBox(mw, "错误", "用户名或密码错误", walk.MsgBoxIconError)
							}
						},
					},
				},
			},
		},
	}.Create()
	setWindowMaxDisable(mw)
	setWindowSizeDisable(mw)
	setWindowIcon(m.logger, mw)
	setOriginLocation(mw, loginGUIWidth, loginGUIHeight)
	setSysTray(m.logger, mw)
	// setSysNotification(logger, mw, "syscloud", "正在连接中")
}
